package in.iqing.view.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.update.UmengUpdateAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.base.BaseActivity;
import in.iqing.control.adapter.MainPagerAdapter;
import in.iqing.control.util.ToastUtil;
import in.iqing.view.fragment.CategoryFragment;
import in.iqing.view.fragment.MyStatusFragment;
import in.iqing.view.fragment.RecommendFragment;

/**
 * Created by neavo on 2014/6/23.
 */

public class MainActivity extends BaseActivity {

    @Bind(R.id.main_pager)
    public ViewPager mainPager;

    @Bind(R.id.recommend_image)
    public ImageView recommendImage;

    @Bind(R.id.recommend_text)
    public TextView recommendText;

    @Bind(R.id.category_image)
    public ImageView categoryImage;

    @Bind(R.id.category_text)
    public TextView categoryText;

    @Bind(R.id.my_status_image)
    public ImageView myStatusImage;

    @Bind(R.id.my_status_text)
    public TextView myStatusText;

    private MainPagerAdapter pagerAdapter;

    private long lastPressTime = -1;

    private static final int RECOMMEND = 0;
    private static final int CATEGORY = 1;
    private static final int MY_STATUS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onInitialize(Bundle savedInstanceState) {
        super.onInitialize(savedInstanceState);
        initUmengTools();
        initPager();
    }

    private void initPager() {
        pagerAdapter = new MainPagerAdapter(getFragmentManager());
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new RecommendFragment());
        fragments.add(new CategoryFragment());
        fragments.add(new MyStatusFragment());
        pagerAdapter.setFragments(fragments);
        mainPager.setAdapter(pagerAdapter);
        mainPager.addOnPageChangeListener(new ExtOnPageChangeListener());
    }

    @Override
    public void onBackPressed() {
        if (lastPressTime != -1 && SystemClock.elapsedRealtime() - lastPressTime < 2000) {
            exit();
        } else {
            lastPressTime = SystemClock.elapsedRealtime();
            ToastUtil.show(getContext(), R.string.activity_main_exit);
        }
    }

    private void initUmengTools() {
        UmengUpdateAgent.setUpdateAutoPopup(true);
        UmengUpdateAgent.setUpdateOnlyWifi(false);
        UmengUpdateAgent.setUpdateListener(null);
        UmengUpdateAgent.setDeltaUpdate(false);
        UmengUpdateAgent.update(getContext());
    }

    @OnClick(R.id.recommend_layout)
    public void onRecommendClick(View v){
        mainPager.setCurrentItem(RECOMMEND);
    }

    @OnClick(R.id.category_layout)
    public void onCategoryClick(View v){
        mainPager.setCurrentItem(CATEGORY);
    }

    @OnClick(R.id.my_status_layout)
    public void onMyStatusClick(View v){
        mainPager.setCurrentItem(MY_STATUS);
    }

    private class ExtOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case RECOMMEND:
                    recommendImage.setImageResource(R.drawable.icon_recommend_selected);
                    recommendText.setTextColor(getResources().getColor(R.color.theme));

                    categoryImage.setImageResource(R.drawable.icon_category);
                    categoryText.setTextColor(getResources().getColor(R.color.secondary_text));

                    myStatusImage.setImageResource(R.drawable.icon_my_status);
                    myStatusText.setTextColor(getResources().getColor(R.color.secondary_text));
                    break;
                case CATEGORY:
                    recommendImage.setImageResource(R.drawable.icon_recommend);
                    recommendText.setTextColor(getResources().getColor(R.color.secondary_text));

                    categoryImage.setImageResource(R.drawable.icon_category_selected);
                    categoryText.setTextColor(getResources().getColor(R.color.theme));

                    myStatusImage.setImageResource(R.drawable.icon_my_status);
                    myStatusText.setTextColor(getResources().getColor(R.color.secondary_text));
                    break;
                case MY_STATUS:
                    recommendImage.setImageResource(R.drawable.icon_recommend);
                    recommendText.setTextColor(getResources().getColor(R.color.secondary_text));

                    categoryImage.setImageResource(R.drawable.icon_category);
                    categoryText.setTextColor(getResources().getColor(R.color.secondary_text));

                    myStatusImage.setImageResource(R.drawable.icon_my_status_selected);
                    myStatusText.setTextColor(getResources().getColor(R.color.theme));
                    break;
            }

        }
    }

}
